﻿use adventureworks
go
if exists(select * from sys.assemblies where name like 'ReportCache.SqlUtility')
begin
	if object_id('spExecScriptAndCreateTable') is not null
		drop procedure spExecScriptAndCreateTable

	if object_id('spReportWithParameters') is not null
		drop procedure spReportWithParameters

	drop ASSEMBLY [ReportCache.SqlUtility]
end
GO

CREATE ASSEMBLY [ReportCache.SqlUtility]
FROM 'f:\temp\ReportCache.SqlUtility.dll'
WITH PERMISSION_SET = SAFE;
GO
/*
Usage:
exec adventureworks.dbo.spExecScript 'adventureworks.dbo.GetProductListing','tempdb','dbo.teszt2','i int',2
select * from tempdb.dbo.teszt2 order by i desc
parameters:
--@executeSql sql command
--@destDbName destination database name
--@tableName  destionation table name
--@additionalFieldNames for new table
-- @additionalFieldValues for new rows
--@enums = 1 if debug
*/
create procedure dbo.spExecScriptAndCreateTable(@executeSql nvarchar(4000),
									@DestDbName sysname, 
									@tableName sysname, 
									@additionalFieldNames nvarchar(1000)='',
                                    @additionalFieldValues nvarchar(1000)='',
									@enums int=1)
AS
EXTERNAL NAME [ReportCache.SqlUtility].[ReportCache].[ExecScriptAndCreateTable]


--